package com.munoor.dalai.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.munoor.dalai.client.event.LoginEvent;
import com.munoor.dalai.client.event.LoginEventHandler;
import com.munoor.dalai.client.model.User;
import com.munoor.dalai.client.presenter.BusyIndicatorPresenter;
import com.munoor.dalai.client.presenter.ControlPanelPresenter;
import com.munoor.dalai.client.presenter.LoginPresenter;
import com.munoor.dalai.client.presenter.UserBadgePresenter;
import com.munoor.dalai.client.view.BusyIndicatorView;
import com.munoor.dalai.client.view.LoginView;
import com.munoor.dalai.client.view.UserBadgeView;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Dalai implements EntryPoint {

	@UiField
	HeaderPanel headerPanel;

	private static Dalai singleton;
	private static DalaiUiBinder uiBinder = GWT.create(DalaiUiBinder.class);
	RootLayoutPanel root;

	private User user = new User();
	private SimpleEventBus eventBus = new SimpleEventBus();
	// Presenters
	private UserBadgePresenter userBadgePresenter;
	private ControlPanelPresenter controlPanelPresenter;
	private BusyIndicatorPresenter busyIndicator = new BusyIndicatorPresenter(eventBus, new BusyIndicatorView("Working hard..."));

	interface DalaiUiBinder extends UiBinder<DockLayoutPanel, Dalai> {
	}

	/*
	 * public Dalai() { initWidget(uiBinder.createAndBindUi(this)); }
	 */

	public static Dalai get() {
		return singleton;

	}

	public void showLoginView() {
		root = RootLayoutPanel.get();
		root.clear();
		LoginPresenter loginPresenter = new LoginPresenter(eventBus, new LoginView());
		loginPresenter.go(root);
	}

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		singleton = this;
		if (user.getUsername() == null) {
			showLoginView();
		}
		eventBus.addHandler(LoginEvent.TYPE, new LoginEventHandler() {
			@Override
			public void onLogin(LoginEvent event) {
				goAfterLogin();
			}
		});
	}

	public SimpleEventBus getEventBus() {
		return eventBus;
	}

	public final User getUser() {
		return user;
	}

	private void goAfterLogin() {
	    DockLayoutPanel outer = uiBinder.createAndBindUi(this);
	    root = RootLayoutPanel.get();
	    root.clear();
	    root.add(outer);

	   AppController appViewer = new AppController(eventBus);
	   appViewer.go();

	    //friendListPresenter = new FriendListPresenter(friendService, messagesService, eventBus, new FriendListView());
	    //friendListPresenter.go(friendListPanel);

	    userBadgePresenter = new UserBadgePresenter(eventBus, new UserBadgeView());
	    userBadgePresenter.go(headerPanel.getuserBadgePanel());
	    
	    controlPanelPresenter = new ControlPanelPresenter(eventBus, new ControlPanel());

	}

}
